#include <iostream>
using namespace std;
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <cstring>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SERVER_PORT 8080
int main()
{
    //创建套接字
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(sockfd == -1)
    {
        perror("socket error");
        _exit(-1);
    }

    
    cout<<"sockfd:"<<sockfd<<endl;
    cout<<"hello world!"<<endl;

    //连接服务器
    struct sockaddr_in serverAddress;
    memset(&serverAddress,0,sizeof(serverAddress));

    serverAddress.sin_family = AF_INET;
    serverAddress.sin_port = htons(SERVER_PORT);

    const char *ip = "192.168.0.101";
    inet_pton(AF_INET,ip,&(serverAddress.sin_addr.s_addr));
    
    int ret = connect(sockfd,reinterpret_cast<const sockaddr *>(&serverAddress),sizeof(serverAddress));
    if(ret != 0)
    {
        perror("connect error:");
        _exit(-1);
    }

    char buffer[128] = {0};
    char readBuffer[128] = {0};

    while(1)
    {
        cout<<"请输入要发送的消息:";
        cin>>buffer;

        write(sockfd,buffer,strlen(buffer)+1);
        sleep(2);

        // memset(buffer,0,128);
        read(sockfd,readBuffer,sizeof(readBuffer));
        cout<<"server:"<<readBuffer<<endl;
    }

    return 0;
}